package cn.hxzy.ownerSource.controller;


import cn.hxzy.common.Result;
import cn.hxzy.entity.order.vo.SelectVo;
import cn.hxzy.entity.sourceEntity.SourceList;
import cn.hxzy.ownerSource.service.SourceListService;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ao.Li
 * @since 2023-06-23
 */
@CrossOrigin
@RestController
@RequestMapping("/sourceList")
public class SourceListController {

    @Autowired
    private SourceListService sourceListService;



    /**
     * 添加货源
     *
     * @param sourceList
     * @return
     */
    @PostMapping("/addSource/{userId}/{pwd}/{money}")
    public Result addSource(@RequestBody SourceList sourceList, @PathVariable String userId, @PathVariable int pwd, @PathVariable Double money) {
        SourceList flag = sourceListService.addSource(sourceList, userId, pwd, money);
        if (flag != null) {
            return Result.ok().message("创建货源成功").data("source", flag);
        }
        return Result.error().message("创建失败");
    }

    /**
     * 根据状态查询货源
     *
     * @return
     */
    @GetMapping("/isSourceListOn/{userId}/{sourceStatus}")
    public Result isSourceListOn(@PathVariable Integer sourceStatus, @PathVariable String userId) {
        List<SourceList> list = sourceListService.isSourceListOn(userId, sourceStatus);
        if (list != null) return Result.ok().message("查找到所有开启状态的货源").data("list", list);
        return Result.error().message("查找开启状态的货源失败");
    }


    ////根据id查询货源列表
    @GetMapping("/selectSourceById/{sourceId}")
    public Result selectSourceById(@PathVariable String sourceId) {
        SourceList sourceList = sourceListService.selectSourceById(sourceId);
        if (sourceList != null) {
            return Result.ok().data1(sourceList).message("查询货源对象成功");
        }
        return Result.error().message("查询货源对象失败");
    }

    //查询所有货源列表
    @GetMapping("/selectSource")
    public Result selectSource() {
        return sourceListService.selectSource();
    }

    @GetMapping("/selectSourceByStatus")
    public Result selectSourceByStatus(){
        return sourceListService.selectSourceByStatus();
    }
    //修改装货量
    @GetMapping("/updateLoadingCount/{loadingCount}/{sourceId}")
    public Result updateLoadingCount(@PathVariable Double loadingCount, @PathVariable String sourceId) {
        return sourceListService.updateLoadingCount(loadingCount, sourceId);
    }

    //修改卸货量
    @GetMapping("/updateUnloadingCount/{unloadingCount}/{sourceId}")
    public Result updateUnloadingCount(@PathVariable Double unloadingCount, @PathVariable String sourceId) {
        return sourceListService.updateUnloadingCount(unloadingCount, sourceId);
    }

    //修改货物余量
    @GetMapping("/updateGoodsHeadroom/{goodsHeadroom}/{sourceId}")
    @ApiOperation(value = "修改货物余量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsHeadroom", value = "货物余量", required = true, dataType = "Double"),
            @ApiImplicitParam(name = "sourceId", value = "货源编号", required = true, dataType = "string"),

    })
    public Result updateGoodsHeadroom(@PathVariable Double goodsHeadroom, @PathVariable String sourceId) {
        return sourceListService.updateGoodsHeadroom(goodsHeadroom, sourceId);
    }

    //多条件查询货源
    @GetMapping("/sourceListAll/{loading}/{unLoading}/{i}")
    public Result sourceListAll(@PathVariable String loading, @PathVariable String unLoading, @PathVariable int i) {
        return sourceListService.selectMoreByTj(loading, unLoading, i);
    }

    /**
     * 修改货源状态
     *
     * @param sourceId
     * @param status
     * @return
     */
    @GetMapping("/updateSourceStatus/{sourceId}/{status}")
    public Result updateSourceStatus(@PathVariable String sourceId, @PathVariable Integer status) {
        Boolean flag = sourceListService.updateSourceStatus(sourceId, status);
        if (flag) return Result.ok().message("修改货源状态成功");
        return Result.error().message("修改货源状态失败");
    }
    @PostMapping("/selectMoreBySiteName")
    public Result selectMoreBySiteName(@RequestBody SelectVo selectVo) throws JsonProcessingException {
        System.out.println("selectvo"+selectVo);
        Result result = sourceListService.selectMoreBySiteName(selectVo);
        System.out.println(result);
        return result;
    }
}

